<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_services_DashBoardService extends f_core_Service
{
	private static $instance;
        private $availableDashBoards;
	/**
	 *
	 * @return m_backoffice_services_DashBoardService
	 */
	public static function getInstance()
	{
		if(self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function getBoards()
	{
		$instanceDir = f_file_Directory::getInstance(MODULES_DIR);
		$modulesDir = $instanceDir->getSubdirs();
		
		foreach($modulesDir as $moduleDir)
		{
			$this->getModuleBoards($moduleDir->getDirName());
		}
        return $this->availableDashBoards;
	}

	public function getModuleBoards($moduleName)
	{
		$xmlFilePath = f_file_Resolver::getModuleFilePath($moduleName, 'config'.DS.'dashBoard.xml');

		if(!is_null($xmlFilePath))
		{
			$xml = f_xml_Xpath::getInstance($xmlFilePath);

			foreach($xml->getElementsFromXpath('board') as $k => $board)
			{
                /* @var $board f_xml_XmlElement */

                //EXISTING DASHBOARD(S) FOR THIS MODULE
                $indexId = isset($this->availableDashBoards[$moduleName]) ? $this->availableDashBoards[$moduleName] : null;
                $index = count($indexId);
                $this->availableDashBoards[$moduleName][$k]['moduleTitle'] = $moduleName;
                $this->availableDashBoards[$moduleName][$k]['title'] = $board->getSubElement('title')->getContent();
                $this->availableDashBoards[$moduleName][$k]['description'] = $board->getSubElement('description')->getContent();
                $this->availableDashBoards[$moduleName][$k]['action'] = $board->getSubElement('action')->getContent();
			}
		}
	}
}
